package com.htx.zqs.blesmartmask.utils;


import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.Html;

import com.htx.zqs.blesmartmask.R;

/**
 * Created by KomoriWu
 * on 2019/2/17.
 */


public class AlertUtils {

    public static void showErrorMessage(Context context, int titleRes,
                                        String errorCode, DialogInterface.OnClickListener listener) {
        if (!((Activity) context).isFinishing()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context)
                    .setMessage(context.getResources().getIdentifier("ERROR_CODE_" + errorCode,
                            "string", context.getPackageName()));
            if (titleRes != 0) {
                builder.setTitle(titleRes);
            }
            if (listener == null) {
                builder.setNegativeButton(R.string.ok1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
            } else {
                builder.setNegativeButton(R.string.ok1, listener);
            }
            Dialog dialog = builder.create();
            dialog.setCancelable(true);
            dialog.show();
        }
    }

    public static void showErrorMessage(Context context, String errorCode) {
        showErrorMessage(context, 0, errorCode, null);
    }

    public static void showErrorMessage(Context context,
                                        String errorCode, DialogInterface.OnClickListener listener) {
        showErrorMessage(context, 0, errorCode, listener);
    }

    public static void showAlertDialog(Context context, String message,
                                       DialogInterface.OnClickListener listener) {
        if (!((Activity) context).isFinishing()) {
            AlertDialog dialog = new AlertDialog.Builder(context)
                    .setMessage(message)
                    .setNegativeButton(R.string.cancel1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setPositiveButton(R.string.confirm, listener)
                    .create();
            dialog.setCancelable(false);
            dialog.show();
        }
    }


    public static void showAlertDialog(Context context, boolean isHtml, int title, String message,
                                       DialogInterface.OnClickListener listener) {
        if (!((Activity) context).isFinishing()) {
            AlertDialog dialog = new AlertDialog.Builder(context)
                    .setTitle(title)
                    .setMessage(isHtml ? Html.fromHtml(message) : message)
                    .setNegativeButton(R.string.cancel1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setPositiveButton(R.string.confirm, listener)
                    .create();
            dialog.setCancelable(true);
            dialog.show();
        }
    }

    public static void showAlertDialog(Context context, boolean isHtml, int title, int cancelId,
                                       int confirmId, String message,
                                       DialogInterface.OnClickListener listener) {
        if (!((Activity) context).isFinishing()) {
            AlertDialog dialog = new AlertDialog.Builder(context)
                    .setTitle(title)
                    .setMessage(isHtml ? Html.fromHtml(message) : message)
                    .setNegativeButton(cancelId, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setPositiveButton(confirmId, listener)
                    .create();
            dialog.setCancelable(true);
            dialog.show();
        }
    }
    public static void showAlertDialog(Context context, int title,int message,boolean isCancel,
                                       DialogInterface.OnClickListener listener) {
        if (!((Activity) context).isFinishing()) {
            AlertDialog dialog = new AlertDialog.Builder(context)
                    .setTitle(title)
                    .setMessage(message)
                    .setNegativeButton(R.string.cancel1, listener)
                    .setPositiveButton(R.string.confirm, listener)
                    .create();
            dialog.setCancelable(isCancel);
            dialog.show();
        }
    }
    //确认跟取消按钮都重写
    public static void showAlertDialog(Context context, int title, int cancelId,
                                       int confirmId, String message,
                                       DialogInterface.OnClickListener listener) {
        if (!((Activity) context).isFinishing()) {
            AlertDialog dialog = new AlertDialog.Builder(context)
                    .setTitle(title)
                    .setMessage(Html.fromHtml(message))
                    .setNegativeButton(cancelId, listener)
                    .setPositiveButton(confirmId, listener)
                    .create();
            dialog.setCancelable(true);
            dialog.show();
        }
    }



    public static void showAlertDialog(Context context, int title, String message,
                                       DialogInterface.OnClickListener listener) {
        showAlertDialog(context, false, title, message, listener);
    }

    public static void showAlertDialog(Context context, int title, String message) {
        if (!((Activity) context).isFinishing()) {
            AlertDialog dialog = new AlertDialog.Builder(context)
                    .setTitle(title)
                    .setMessage(message)
                    .setNegativeButton(R.string.ok1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    })
                    .create();
            dialog.setCancelable(true);
            dialog.show();
        }
    }

    public static void showAlertDialog(Context context, String message) {
        if (!((Activity) context).isFinishing()) {
            AlertDialog dialog = new AlertDialog.Builder(context)
                    .setMessage(message)
                    .setPositiveButton(R.string.ok1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .create();
            dialog.setCancelable(true);
            dialog.show();
        }
    }

    public static void showAlertDialog(Context context, int messageRes) {
        if (!((Activity) context).isFinishing()) {
            AlertDialog dialog = new AlertDialog.Builder(context)
                    .setMessage(messageRes)
                    .setPositiveButton(R.string.ok1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .create();
            dialog.setCancelable(true);
            if (!dialog.isShowing()) {
                dialog.show();
            }
        }
    }


    public static void showProgressDialog(Context context, int id) {
        ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setMessage(context.getString(id));
        progressDialog.show();
    }

   
}
